package net.cgsoft.widget;

/**
 * Created by Youga on 2016/1/9.
 */

import android.app.Notification;
import android.app.Service;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.RemoteViews;
import android.widget.TextView;
import android.widget.Toast;

import net.cgsoft.widget.https.DownCallBack;
import net.cgsoft.widget.service.DownService;
import net.cgsoft.widget.utils.Util;

import java.io.File;

/**
 * Created by Youga on 2016/1/9.
 */
public class DownFragment extends DialogFragment {


    public static final String TAG = "DownFragment";
    TextView title;
    AnimatedCircleLoadingView circleLoadingView;
    private static final String[] EXTRA_PARAM = {"URL", "TITLE"};
    private String mUrl, mTitle;

    public static DownFragment newInstance(String url, String title) {
        DownFragment fragment = new DownFragment();
        Bundle bundle = new Bundle();
        bundle.putString(EXTRA_PARAM[0], url);
        bundle.putString(EXTRA_PARAM[1], title);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (getDialog() != null) {
            getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        }
        return inflater.inflate(R.layout.fragment_down, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        circleLoadingView = (AnimatedCircleLoadingView) view.findViewById(R.id.circle_loading_view);
        title = (TextView) view.findViewById(R.id.title);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mUrl = getArguments().getString(EXTRA_PARAM[0]);
        mTitle = getArguments().getString(EXTRA_PARAM[1]);
        title.setText(mTitle);
        Intent intent = new Intent(getActivity(), DownService.class);
        getActivity().bindService(intent, connection, Service.BIND_AUTO_CREATE);
        circleLoadingView.startDeterminate();
    }

    private DownService mDownService;
    ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mDownService = ((DownService.DownBinder) service).getService();
            mDownService.handleActionDown(mUrl, mTitle, new DownCallBack() {
                @Override
                public void downStart() {

                }

                @Override
                public void downIng(long progress, String size) {
                    circleLoadingView.setPercent((int) progress);
                }

                @Override
                public void downFinish(File apkFile) {
                    Log.i(TAG, "apkFile:" + apkFile.getAbsolutePath());
                }
            });
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        if (!"100%".equals(circleLoadingView.getPercent())) {
            mDownService.publishNotification();
            Toast.makeText(getActivity(), mTitle + "后台下载中...", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unbindService(connection);
    }
}
